package main

import (
	"fmt"
	"mongo/configs"
	"mongo/jobs"
	"mongo/logs"
	"reflect"
)

func main() {

	config := configs.GetConfig()
	if config == nil {
		fmt.Println("the sysconfig read failure.")
		return
	}
	if _, ok := config.SysConfig.Items["languo"]; !ok {
		fmt.Println("the sysconfig languo config was not set.")
		return
	}
	if _, ok := config.SysConfig.Items["openPlatform"]; !ok {
		fmt.Println("the sysconfig openPlatform config was not set.")
		return
	}

	logs.LoadAppConfig()
	logs.Logger.Info("hello wha")
	languo := config.SysConfig.Items["languo"]
	openPlatform := config.SysConfig.Items["openPlatform"]
	defaultCron := "0 0 0 1 * *"
	jobLanGuo := jobs.New(languo.Connection, languo.Name, If(isEmpty(languo.Cron), defaultCron, languo.Cron))
	jobOpenplatform := jobs.New(openPlatform.Connection, openPlatform.Name, If(isEmpty(openPlatform.Cron), defaultCron, openPlatform.Cron))
	scheduleService := jobs.NewService(jobLanGuo, jobOpenplatform)
	scheduleService.Start()
	fmt.Println("deal end.")
}

func If(condition bool, trueVal, falseVal string) string {
	if condition {
		return trueVal
	}
	return falseVal
}

func isEmpty(params interface{}) bool {
	var (
		flag         bool = true
		defaultValue reflect.Value
	)
	r := reflect.ValueOf(params)
	defaultValue = reflect.Zero(r.Type())
	if !reflect.DeepEqual(r.Interface(), defaultValue.Interface()) {
		flag = false
	}
	return flag
}
